package model.bullet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import model.cell.tower.AbstractTower;
import model.enemy.AbstractEnemy;



public abstract class AbstractBullet {
		
	protected BulletThread _bulletThread = new BulletThread();
	
	protected AbstractEnemy _enemy;
	
	protected AbstractTower _tower;
	
	protected float _posX;
	
	protected float _posY;
	
	public Color color = Color.GREEN;
	
	protected Float _speedCoef = new Float(1);
	
	protected int _damage = 10;
	
	protected int _refreshSpeed = 10;
	
	
		
	public AbstractBullet(Point pos, AbstractEnemy enemy, AbstractTower tower) {
		
		_posX = pos.x;
		_posY = pos.y;
		_enemy = enemy;
		_tower = tower;
		
		_bulletThread.start();
	}
	
	
	public abstract void draw(Graphics g);
	
	
	public abstract void move(); 
	
	
	protected void _hitEnemy() {
		
		_enemy.hit(_damage);
	}
	
	
	protected synchronized void _findNewTarget() {
		
		AbstractEnemy enemy = _tower.findEnemy();
		
		if ( null == enemy ) {
			_destroy();
			return;
		}
		
		_enemy = enemy;
	}
	
	
	protected void _destroy() {
		
		_bulletThread.active = false;
		_bulletThread.interrupt();
		_tower.bulletIsDead(this);
	}
	
	
	class BulletThread extends Thread {
		
		public boolean active = true;
		
		public void run() {
			while(active) {
				try {
					move();
					sleep( _refreshSpeed );
				} catch ( Exception e ) {} 
			}
		}
	}
	
}
